<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属机构" prop="orgId">
        <el-input
          v-model="queryParams.orgId"
          placeholder="请输入所属机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="relateUnitCode">
        <el-input
          v-model="queryParams.relateUnitCode"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="relateUnitName">
        <el-input
          v-model="queryParams.relateUnitName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="relateUnitSimpName">
        <el-input
          v-model="queryParams.relateUnitSimpName"
          placeholder="请输入简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:relateUnit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:relateUnit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:relateUnit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:relateUnit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="relateUnitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" v-if="true"/>
      <el-table-column label="所属机构" align="center" prop="orgId" />
      <el-table-column label="编码" align="center" prop="relateUnitCode" />
      <el-table-column label="名称" align="center" prop="relateUnitName" />
      <el-table-column label="简称" align="center" prop="relateUnitSimpName" />
      <el-table-column label="地区" align="center" prop="districtId" />
      <el-table-column label="行业类型" align="center" prop="trade" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="邮编" align="center" prop="postCode" />
      <el-table-column label="注册号" align="center" prop="regCode" />
      <el-table-column label="法人" align="center" prop="corporation" />
      <el-table-column label="传真" align="center" prop="fax" />
      <el-table-column label="邮件" align="center" prop="email" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:relateUnit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:relateUnit:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改往来单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width=600px append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
           <el-col :span=12>
            <el-form-item label="所属机构" prop="orgId">
              <el-input v-model="form.orgId" placeholder="请输入所属机构" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="编码" prop="relateUnitCode">
              <el-input v-model="form.relateUnitCode" placeholder="请输入编码" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="名称" prop="relateUnitName">
              <el-input v-model="form.relateUnitName" placeholder="请输入名称" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="简称" prop="relateUnitSimpName">
              <el-input v-model="form.relateUnitSimpName" placeholder="请输入简称" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="行业类型" prop="trade">
              <el-input v-model="form.trade" placeholder="请输入行业类型" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="邮编" prop="postCode">
              <el-input v-model="form.postCode" placeholder="请输入邮编" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="注册号" prop="regCode">
              <el-input v-model="form.regCode" placeholder="请输入注册号" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="法人" prop="corporation">
              <el-input v-model="form.corporation" placeholder="请输入法人" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="邮件" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮件" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
:label="dict.value"                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
           </el-col>
    <el-col :span=12>
        <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
    </el-col>
        <el-divider content-position="center">往来单位明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddRelateUnitDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteRelateUnitDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="relateUnitDetailList" :row-class-name="rowRelateUnitDetailIndex" @selection-change="handleRelateUnitDetailSelectionChange" ref="relateUnitDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="所属机构" prop="orgId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orgId" placeholder="请输入所属机构" />
            </template>
          </el-table-column>
          <el-table-column label="类型" prop="relateUnitType">
            <template slot-scope="scope">
              <el-input v-model="scope.row.relateUnitType" placeholder="请输入类型" />
            </template>
          </el-table-column>
          <el-table-column label="银行" prop="bank">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bank" placeholder="请输入银行" />
            </template>
          </el-table-column>
          <el-table-column label="银行账号" prop="bankAccount">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bankAccount" placeholder="请输入银行账号" />
            </template>
          </el-table-column>
          <el-table-column label="手机" prop="mobileTel">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mobileTel" placeholder="请输入手机" />
            </template>
          </el-table-column>
          <el-table-column label="联系人" prop="relatePersonId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.relatePersonId" placeholder="请输入联系人" />
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="deptId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deptId" placeholder="请输入部门" />
            </template>
          </el-table-column>
          <el-table-column label="专管员" prop="employeeId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.employeeId" placeholder="请输入专管员" />
            </template>
          </el-table-column>
          <el-table-column label="折扣率" prop="discRate">
            <template slot-scope="scope">
              <el-input v-model="scope.row.discRate" placeholder="请输入折扣率" />
            </template>
          </el-table-column>
          <el-table-column label="信用等级" prop="creditGrade">
            <template slot-scope="scope">
              <el-input v-model="scope.row.creditGrade" placeholder="请输入信用等级" />
            </template>
          </el-table-column>
          <el-table-column label="信用额度" prop="creditLine">
            <template slot-scope="scope">
              <el-input v-model="scope.row.creditLine" placeholder="请输入信用额度" />
            </template>
          </el-table-column>
          <el-table-column label="信用期限" prop="creditDate">
            <template slot-scope="scope">
              <el-input v-model="scope.row.creditDate" placeholder="请输入信用期限" />
            </template>
          </el-table-column>
          <el-table-column label="支付方式" prop="payMethodId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.payMethodId" placeholder="请输入支付方式" />
            </template>
          </el-table-column>
          <el-table-column label="价格清单" prop="priceBillId">
            <template slot-scope="scope">
              <el-input v-model="scope.row.priceBillId" placeholder="请输入价格清单" />
            </template>
          </el-table-column>
          <el-table-column label="期初应收/付" prop="paid">
            <template slot-scope="scope">
              <el-input v-model="scope.row.paid" placeholder="请输入期初应收/付" />
            </template>
          </el-table-column>
          <el-table-column label="期初其他应收/付" prop="otherPaid">
            <template slot-scope="scope">
              <el-input v-model="scope.row.otherPaid" placeholder="请输入期初其他应收/付" />
            </template>
          </el-table-column>
          <el-table-column label="期初预收/付" prop="prePaid">
            <template slot-scope="scope">
              <el-input v-model="scope.row.prePaid" placeholder="请输入期初预收/付" />
            </template>
          </el-table-column>
          <el-table-column label="业务余额" prop="leavePaid">
            <template slot-scope="scope">
              <el-input v-model="scope.row.leavePaid" placeholder="请输入业务余额" />
            </template>
          </el-table-column>
          <el-table-column label="发运目的地" prop="transTo">
            <template slot-scope="scope">
              <el-input v-model="scope.row.transTo" placeholder="请输入发运目的地" />
            </template>
          </el-table-column>
          <el-table-column label="发运提前期" prop="transAdvance">
            <template slot-scope="scope">
              <el-input v-model="scope.row.transAdvance" placeholder="请输入发运提前期" />
            </template>
          </el-table-column>
          <el-table-column label="发运方式" prop="transType">
            <template slot-scope="scope">
              <el-input v-model="scope.row.transType" placeholder="请输入发运方式" />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template slot-scope="scope">
              <el-input v-model="scope.row.status" placeholder="请输入状态" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(false)">保 存</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(true)">保存并新增</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRelateUnit, getRelateUnit, delRelateUnit, addRelateUnit, updateRelateUnit } from "@/api/base/relateUnit";

export default {
  name: "RelateUnit",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRelateUnitDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 往来单位表格数据
      relateUnitList: [],
      // 往来单位明细表格数据
      relateUnitDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: undefined,
        relateUnitCode: undefined,
        relateUnitName: undefined,
        relateUnitSimpName: undefined,
        districtId: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        orgId: [
          { required: true, message: "所属机构不能为空", trigger: "blur" }
        ],
        relateUnitCode: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
        relateUnitName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        relateUnitSimpName: [
          { required: true, message: "简称不能为空", trigger: "blur" }
        ],
        districtId: [
          { required: true, message: "地区不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询往来单位列表 */
    getList() {
      this.loading = true;
      listRelateUnit(this.queryParams).then(response => {
        this.relateUnitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 保存并新增操作 */
    handleSaveAdd() {
      this.reset();
      this.getTreeselect();
      this.title = "添加往来单位";
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orgId: undefined,
        relateUnitCode: undefined,
        relateUnitName: undefined,
        relateUnitSimpName: undefined,
        districtId: undefined,
        trade: undefined,
        address: undefined,
        postCode: undefined,
        regCode: undefined,
        corporation: undefined,
        fax: undefined,
        email: undefined,
        phone: undefined,
        status: "0",
        delFlag: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        tenantId: undefined
      };
      this.relateUnitDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加往来单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getRelateUnit(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.relateUnitDetailList = response.data.relateUnitDetailList;
        this.open = true;
        this.title = "修改往来单位";
      });
    },
    /** 提交按钮 */
    submitForm(isAdd) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.relateUnitDetailList = this.relateUnitDetailList;
          if (this.form.id != null) {
            updateRelateUnit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              if (isAdd){
                  this.handleSaveAdd();
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addRelateUnit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              if (isAdd){
                  this.handleSaveAdd();
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除往来单位编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delRelateUnit(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
	/** 往来单位明细序号 */
    rowRelateUnitDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 往来单位明细添加按钮操作 */
    handleAddRelateUnitDetail() {
      let obj = {};
      obj.orgId = "";
      obj.relateUnitType = "";
      obj.bank = "";
      obj.bankAccount = "";
      obj.mobileTel = "";
      obj.relatePersonId = "";
      obj.deptId = "";
      obj.employeeId = "";
      obj.discRate = "";
      obj.creditGrade = "";
      obj.creditLine = "";
      obj.creditDate = "";
      obj.payMethodId = "";
      obj.priceBillId = "";
      obj.paid = "";
      obj.otherPaid = "";
      obj.prePaid = "";
      obj.leavePaid = "";
      obj.transTo = "";
      obj.transAdvance = "";
      obj.transType = "";
      obj.status = "";
      obj.remark = "";
      this.relateUnitDetailList.push(obj);
    },
    /** 往来单位明细删除按钮操作 */
    handleDeleteRelateUnitDetail() {
      if (this.checkedRelateUnitDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的往来单位明细数据");
      } else {
        const relateUnitDetailList = this.relateUnitDetailList;
        const checkedRelateUnitDetail = this.checkedRelateUnitDetail;
        this.relateUnitDetailList = relateUnitDetailList.filter(function(item) {
          return checkedRelateUnitDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleRelateUnitDetailSelectionChange(selection) {
      this.checkedRelateUnitDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/relateUnit/export', {
        ...this.queryParams
      }, `relateUnit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
